/**
 * Created with JetBrains PhpStorm.
 * User: jiangshuguang
 * Date: 14-9-27
 * Time: 下午4:22
 * To change this template use File | Settings | File Templates.
 */
(function(){
    var util = require("wap_common:widget/ui/libs/util.js").util,
        $page = $(".main-register"),
        $tip = $page.find(".tip"),
        $username = $page.find(".phone"),
        $code = $page.find(".check-code"),
        $password = $page.find(".password"),
        $confirmPassword = $page.find(".confirm-password"),
        $registerFrom = $page.find(".register-from"),
        $recommender = $page.find(".recommender"),
        $phoneBtn = $page.find(".btn-phone"),
        $registerBtn = $page.find(".btn-register");

    function tipShow(text){
        $tip.css("visibility","visible").text(text);
    }

    function tipHide(){
        $tip.css("visibility","hidden").text("");
    }

    //前段验证
    function check(){
        tipHide();
        if(!util.checkPhone($username.val())){
            tipShow("手机号码输入错误");
            return false;
        }else if(!(/^\d{6}$/.test($code.val()))){
            tipShow("验证码输入错误");
            return false;
        }else if(!util.checkPassword($password.val())){
            tipShow("密码不是6-16位字符");
            return false;
        }
//        else if($password.val() != $confirmPassword.val()){
//            tipShow("两次密码输入不一致");
//            return false;
//        }
        else if(!!($recommender.val()) && !(util.checkPhone($recommender.val()))){
            tipShow("推荐人手机填写错误");
            return false;
        }else if(!$("#protocol:checked").length){
            tipShow("请确认同意真融宝协议");
            return false;
        }else{
            return true;
        }
    }


    $phoneBtn.on("click",function(){
         if(!($(this).hasClass("btn-disable"))){
             var username = $.trim($username.val());
             if(util.checkPhone(username)){
                 tipHide();
                 $phoneBtn.addClass("btn-phone-disable").text("正在获取");
                 $.post("/account/sendidentitycodewap",{
                     mobile:username,
                     type:$phoneBtn.attr("data-type")
                 },function(ret,status){
                     if(status == "success"){
                         if(ret.error_no == 0){
                             var count = 60,
                                 countInterval = setInterval(function(){
                                     $phoneBtn.text(count);
                                     if(count>0){
                                         count--;
                                     }else{
                                         count = 60;
                                         clearInterval(countInterval);
                                         $phoneBtn.removeClass("btn-phone-disable")
                                             .text("未收到,语音获取")
                                             .attr("data-type",1);
                                     }
                                 },1000);
                         }else{
                             tipShow(ret.error_message);
                         }
                     }else{
                         tipShow("网络错误,请重试");
                         $phoneBtn.removeClass("btn-phone-disable").text("重复获取");
                     }
                 },"json")
             }else{
                 tipShow("手机号码输入错误");
             }
         }
     });

    $registerBtn.on("click",function(){
        var me = this;
        if( !($(this).hasClass("btn-disable"))){
            if(check()){
                $(this).addClass("btn-disable");
                $.post("/activity/registeruser",{
                    user_name:    $.trim($username.val()),
                    code:         $.trim($code.val()),
                    passwd:       $.trim($password.val()),
                    register_from:$registerFrom.val()
                },function(ret,status){
                    if(status == "success"){
                        if( ret.error_no == 0){
                            location.href="/activity/double12list";
                        }else{
                            $(me).removeClass("btn-disable");
                            tipShow(ret.error_message);
                        }
                    }else{
                        $(me).removeClass("btn-disable");
                        tipShow("网络错误,请重试");
                    }
                    $registerBtn.removeClass("btn-phone-disable");
                },"json")
            }
        }
    })
})();